package com.easy.work.function.me.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.base.syslib.view.XCFlowLayout;
import com.easy.work.R;
import com.easy.work.bean.WorkListBean;
import com.easy.work.function.main.WorkBaseDetailAct;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author zhangfeng
 * @date 2017/12/26
 * @desc 我的工作列表适配器
 */

public class MyWorkListAdapter extends ArrayAdapter<WorkListBean.ListEntity> {

    private List<WorkListBean.ListEntity> mList;
    private LayoutInflater mInflater;
    private Context mContext;
    ViewGroup.MarginLayoutParams marginParams;

    public MyWorkListAdapter(Context context, int resource, List<WorkListBean.ListEntity> objects) {
        super(context, resource, objects);
        mList = objects;
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    /**
     * 刷新数据
     */
    public void refreshData(List<WorkListBean.ListEntity> list) {
        mList.clear();
        addMoreData(list);
    }

    public void addMoreData(List<WorkListBean.ListEntity> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearData() {
        mList.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_work_condition_list, parent, false);
            holder = new ViewHolder(convertView);

            ViewGroup.LayoutParams params = holder.rlXCFlowLayout.getLayoutParams();
            //获取view的margin设置参数
            if (params instanceof ViewGroup.MarginLayoutParams) {
                marginParams = (ViewGroup.MarginLayoutParams) params;
            } else {
                //不存在时创建一个新的参数
                //基于View本身原有的布局参数对象
                marginParams = new ViewGroup.MarginLayoutParams(params);
            }
            marginParams.rightMargin = 20;
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final WorkListBean.ListEntity entity = mList.get(position);

        String[] data = entity.welfareList;
        if (data != null && data.length > 01) {
            holder.welfareType.removeAllViews();
            for (int i = 0; i < data.length; i++) {
                final TextView view = new TextView(mContext);
                view.setTextSize(mContext.getResources().getDimension(R.dimen.xcflowlayout_tv_size));
                view.setBackground(mContext.getResources().getDrawable(R.drawable.corner_bg_gray));
                view.setTextColor(mContext.getResources().getColor(R.color.gray_bg_999));
                view.setGravity(Gravity.CENTER);
                view.setText(data[i]);

                holder.welfareType.addView(view, marginParams);
            }
        }

        Picasso.with(mContext).load(entity.companyImage).into(holder.ivCompany);
        holder.tvWorkContent.setText(getStr(entity.title));
        holder.tvSalary.setText(entity.salaryFrom + "-" + entity.salaryTo + "元/月");
//        holder.tvWorkAddress.setText("上海市  静安区");
        holder.tvWorkAddress.setText(entity.areaText);


        holder.tvState.setText("状态：" + entity.statusText);

        holder.rl_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WorkBaseDetailAct.start(mContext, entity.workId);
            }
        });
        return convertView;
    }

    private String getStr(String str) {
        return TextUtils.isEmpty(str) ? "" : str;
    }

    class ViewHolder {
        @BindView(R.id.rl_item)
        RelativeLayout rl_item;
        @BindView(R.id.iv_company)
        ImageView ivCompany;
        @BindView(R.id.tv_work_content)
        TextView tvWorkContent;
        @BindView(R.id.tv_salary)
        TextView tvSalary;
        @BindView(R.id.tv_work_address)
        TextView tvWorkAddress;
        @BindView(R.id.tv_state)
        TextView tvState;
        @BindView(R.id.welfare_type)
        XCFlowLayout welfareType;
        @BindView(R.id.rl_XCFlowLayout)
        RelativeLayout rlXCFlowLayout;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
